package com.example.workbench.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.workbench.common.api.BusinessException;
import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddTaskCommand;
import com.example.workbench.common.command.UpdateTaskCommand;
import com.example.workbench.common.config.shiro.UserUtil;
import com.example.workbench.entity.DbTask;
import com.example.workbench.mapper.DbTaskMapper;
import com.example.workbench.service.IDbTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 看板中心表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-08-03
 */
@Service
public class DbTaskServiceImpl extends ServiceImpl<DbTaskMapper, DbTask> implements IDbTaskService {
    @Autowired
    private UserUtil userUtil;
    @Override
    public CommonResult addTask(AddTaskCommand command) {
        Long userId = userUtil.getUserId();
        DbTask dbTask = queryTask(command.getTaskName());
        System.out.println(dbTask);
        if (null != dbTask) {
            return CommonResult.failed("该看板名称已存在");
        }
        System.out.println("cs2");
        DbTask addTask = new DbTask(command.getTaskName(), userId, command.getBgColor());
        getBaseMapper().insert(addTask);
        System.out.println("cs3");
        return CommonResult.success("");
    }

    @Override
    public CommonResult delTask(Long id) {
        Long userId = userUtil.getUserId();
        DbTask dbTask = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTask>()
                .eq(DbTask::getUserId, userId)
                .eq(DbTask::getId, id)
                .eq(DbTask::getIsDelete, 0));
        if (null == dbTask) {
            return CommonResult.failed("该看板不存在,无法删除");
        }
        getBaseMapper().deleteById(dbTask);
        return CommonResult.success("");
    }

    @Override
    public CommonResult updateTask(UpdateTaskCommand command) {
        Long userId = userUtil.getUserId();
        DbTask dbTask = getBaseMapper().selectOne(new LambdaQueryWrapper<DbTask>()
                .eq(DbTask::getUserId, userId)
                .eq(DbTask::getId, command.getId())
                .eq(DbTask::getIsDelete, 0));
        if (null == dbTask) {
            return CommonResult.failed("该看板不存在,无法修改");
        }
        getBaseMapper().deleteById(dbTask);
        dbTask.setTaskName(command.getTaskName());
        getBaseMapper().updateById(dbTask);
        return CommonResult.success("");
    }

    @Override
    public List<DbTask> queryTask() {
        Long userId = userUtil.getUserId();
        List<DbTask> list = getBaseMapper().selectList(new LambdaQueryWrapper<DbTask>()
                .eq(DbTask::getUserId, userId)
                .eq(DbTask::getIsDelete, 0)
                .orderByDesc(DbTask::getCreateTime));
        return list;
    }

    public DbTask queryTask(String taskName) {
        Long userId = userUtil.getUserId();
//        查询是否已经存在
        return getBaseMapper().selectOne(new LambdaQueryWrapper<DbTask>()
                .eq(DbTask::getUserId, userId)
                .eq(DbTask::getTaskName, taskName)
                .eq(DbTask::getIsDelete, 0));
    }
}
